package com.maszy.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.maszy.bean.Breakfast;
import com.maszy.bean.Dept;
import com.maszy.bean.DoorStore;
import com.maszy.bean.GoodPlan;
import com.maszy.bean.Product;
import com.maszy.commons.tools.PrintGoodPlanReport4;
import com.maszy.commons.tools.PrintTool;
import com.maszy.service.BreakfastService;
import com.maszy.service.CashRegisterService;
import com.maszy.service.DeptService;
import com.maszy.service.DoorStoreService;
import com.maszy.service.GoodPlanService;
import com.maszy.service.ProductService;

public class GoodPlanServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	GoodPlanService service=new GoodPlanService();
	String view = new String(); // 页面路径

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		GoodPlanServlet servlet = new GoodPlanServlet();
		RequestDispatcher rd = null;
		String method = request.getParameter("method");
		String status="1";
	if ("queryAll".equals(method)) {
			// 查询出全部数据
			servlet.queryAll(request, response);
			view = "/background/goodplan/goodPlanTable.jsp";
		} else if ("insert".equals(method)) {
			// 添加新记录
			servlet.add(request, response);			
			view = "/background/goodplan/message.jsp";
			status="2";
		} else if ("update".equals(method)) {
			// 修改记录
			servlet.update(request, response);
			servlet.queryAll(request, response);
			view = "/background/product/product.jsp";
		} else if ("delete".equals(method)) {
			// 删除记录
			servlet.delete(request, response);
			servlet.queryAll(request, response);
		} else if ("queryForOne".equals(method)) {
			// 进入修改页面
			servlet.queryForOne(request, response);
			view = "/background/goodplan/goodPlanItem.jsp";
		} else if ("insertForward".equals(method)) {
			// 进入添加页面
			servlet.insertForward(request, response);
			view = "/background/goodplan/toGoodPlan.jsp";
		} else if ("queryAllForward".equals(method)) {
			// 进入查询页面			
			servlet.queryAllForward(request, response);
			view = "/background/goodplan/goodplanMain.jsp";
		}else if ("queryReport1".equals(method)) {
			// 进入查询页面			
			servlet.queryReport1(request, response);
			view = "/background/goodplan/goodplanReport1.jsp";
		}else if ("queryReport4".equals(method)) {
			// 进入查询页面			
			servlet.queryReport4(request, response);
			view = "/background/goodplan/goodplanReport2.jsp";
		}
		else if ("doorQueryForward".equals(method)) {
			// 进入查询页面			
//			servlet.doorQueryForward(request, response);
			view = "/background/goodplan/storeQuery.jsp";
		}
		try {
			if("1".equals(status)){
				rd = this.getServletContext().getRequestDispatcher(view);
				rd.forward(request, response);
			}else{
				response.sendRedirect(request.getContextPath()+view);
			}
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void doorQueryForward(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		DoorStoreService ds=new DoorStoreService();
		List<DoorStore> list=ds.queryAll();
		request.setAttribute("list", list);
	}
	private void insertForward(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		GoodPlan obj=new GoodPlan();
		if(request.getParameter("GoodPlan_Type")!=null){
			request.setAttribute("goodtype", request.getParameter("GoodPlan_Type"));
			String date=new SimpleDateFormat("yyyy-MM-dd").format(Calendar
					.getInstance().getTime());
//			String date="2012-6-17";
			obj.setGoodPlan_BeginDate(date+" 00:00:00");
			obj.setGoodPlan_EndDate(date+" 23:59:59");	
			obj.setGoodPlan_Type(request.getParameter("GoodPlan_Type"));
			obj.setDoorStore_Id(Long.parseLong(request.getParameter("DoorStore_Id")));
			obj=service.queryGoodPlanByDoorStoreAndType(obj);
		
			if(obj.getGoodPlan_Id()!=null&&!"5".equals(request.getParameter("GoodPlan_Type"))){
				obj=service.queryPlanBodyByGoodPlanId(obj);
			}

			request.setAttribute("obj",obj);
			
		}else{
			request.setAttribute("showMessage", "1");
		}
		DoorStoreService ds=new DoorStoreService();
		List<DoorStore> list=ds.queryAll();
		request.setAttribute("list", list);
		if("4".equals(request.getParameter("GoodPlan_Type"))||"5".equals(request.getParameter("GoodPlan_Type"))){
			BreakfastService service = new BreakfastService();
			List<Breakfast> breakfastList=service.queryAllByStatus();
			request.setAttribute("breakfastList", breakfastList);
		}else{
			ProductService ps=new ProductService();
			List<Product> proList=ps.queryAll();
			DeptService deptService=new DeptService();
			List<Dept> deptList=deptService.queryAll();
			List<GoodPlan> goodPlanList=new ArrayList<GoodPlan>();		
			for(int i=0;i<deptList.size();i++){
				int dept_id=deptList.get(i).getDept_id();
				List<Product> tempList=new ArrayList<Product>();
				for(int j=0;j<proList.size();j++){
					if(dept_id==proList.get(j).getProduct_Dept_Id()&&"1".equals(proList.get(j).getProduct_Status())){
						tempList.add(proList.get(j));
					}
				}
				GoodPlan goodPlan=new GoodPlan();
				goodPlan.setDept_Name(deptList.get(i).getDept_name());
				goodPlan.setProductList(tempList);
				goodPlanList.add(goodPlan);
			}
			request.setAttribute("goodPlanList", goodPlanList);
		}
		
	}
	
	private void queryAllForward(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		DoorStoreService dss=new DoorStoreService();
		List dssList=dss.queryAll();
		request.setAttribute("dssList",dssList);
		
	}
	private void delete(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
//		String id=request.getParameter("product_Id");
//		if(!"".equals(id)&&id!=null){
//			Product obj=new Product();
//			obj.setProduct_Id(Long.parseLong(id));
//			service.delete(obj);
//		}
	}

	private void queryForOne(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		GoodPlan obj=new GoodPlan();
		String id=request.getParameter("id");		
		if(!"".equals(id)&&id!=null){		
			obj.setGoodPlan_Id(Long.parseLong(id));			
			obj=service.PrintGoodPlan(obj);
			obj=changeGoodPlan(obj);
			request.setAttribute("item",obj);
			
		}
	}

	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
//		Product obj=new Product();
//		String id=request.getParameter("product_Id");		
//		if(!"".equals(id)&&id!=null){		
//			obj.setProduct_Id(Long.parseLong(id));	
//			obj.setProduct_Name(request.getParameter("Product_Name"));
//			obj.setProduct_Unit(request.getParameter("Product_Unit"));
//			obj.setProduct_Remark(request.getParameter("Product_Remark"));
//			service.update(obj);
//		}
	}

	private void queryAll(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String doorStore_Id=request.getParameter("DoorStore_Id");
		String ToGoodType=request.getParameter("ToGoodType");
		String time=request.getParameter("time");
		String time2=request.getParameter("time2");
		GoodPlan obj=new GoodPlan();	
		obj.setDoorStore_Id(Long.parseLong(doorStore_Id));
		obj.setGoodPlan_Type(ToGoodType);
		obj.setGoodPlan_BeginDate(time);
		
		List list = service.queryAll(obj);
		request.setAttribute("list", list);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		GoodPlan obj=new GoodPlan();	
		String message="";
		String doorstore_id=request.getParameter("DoorStore_Id");
		if(!"".equals(doorstore_id)&&doorstore_id!=null){	
			obj.setDoorStore_Id(Long.parseLong(doorstore_id));
			obj.setGoodPlan_User(request.getParameter("GoodPlan_User"));
			obj.setGoodPlan_Type(request.getParameter("GoodPlan_Type"));
		}
		String goodPlan_id=request.getParameter("GoodPlan_Id");
		Long i=0l;
		if(!"".equals(goodPlan_id)&&goodPlan_id!=null){
			obj.setGoodPlan_Id(Long.parseLong(goodPlan_id));
			i=service.deletePlanBodyByGoodPlanId(obj);
		}else{
			i=service.add(obj);
		}
		//System.out.println(request.getParameter("GoodPlan_Type"));
		
		//插入成功=1，插入失败=0
		if(i>0){		
			obj.setGoodPlan_Id(i);
			Product product=new Product();
			String[] quantity=request.getParameterValues("quantity");
			String[] id=request.getParameterValues("id");
			String[] name=request.getParameterValues("name");
			String[] specification=request.getParameterValues("pecification");
			String[] unit=request.getParameterValues("unit");
			String[] remark=request.getParameterValues("remark");
			String[] dept_name=request.getParameterValues("dept_name");
			String[] quantity2=request.getParameterValues("PlanBody_Quantity");
			String[] id2=request.getParameterValues("PlanBody_Id");
			String[] name2=request.getParameterValues("PlanBody_Name");
			String[] unit2=request.getParameterValues("PlanBody_Unit");
			String[] remark2=request.getParameterValues("PlanBody_Remark");
			String[] dept_name2=request.getParameterValues("dept_name2");
			String[] specification2=request.getParameterValues("PlanBody_Specification");
			message="原材料供应单已提交成功,如果无需其他操作请退出系统。";
			for(int m=0;m<quantity.length;m++){
				if(!"".equals(quantity[m])&&!"0".equals(quantity[m])&&quantity[m]!=null){
					if(id!=null){
						product.setProduct_Id(Long.parseLong(id[m]));
					}else{
						product.setProduct_Id(0L);
					}
					product.setProduct_Name(name[m]);
					product.setProduct_Specification(specification[m]);
					product.setProduct_Unit(unit[m]);
					product.setProduct_Quantity(quantity[m]);
					product.setProduct_Remark(remark[m]);
					product.setProduct_Dept_Name(dept_name[m]);
					obj.setProduct(product);
					service.addPlanBody(obj);
				}				
			}
			if(quantity2!=null){
			for(int m=0;m<quantity2.length;m++){
				if(!"".equals(quantity2[m])&&!"0".equals(quantity2[m])&&quantity2[m]!=null){
					if(id2!=null){
						product.setProduct_Id(Long.parseLong(id2[m]));
					}else{
						product.setProduct_Id(0L);
					}
					product.setProduct_Name(name2[m]);
					product.setProduct_Specification(specification2[m]);
					product.setProduct_Unit(unit2[m]);
					product.setProduct_Quantity(quantity2[m]);
					product.setProduct_Remark(remark2[m]);
					product.setProduct_Dept_Name(dept_name2[m]);
					obj.setProduct(product);
					service.addPlanBody(obj);
				}				
			}
			}
			request.getSession().setAttribute("message", message);
			if("5".equals(obj.getGoodPlan_Type())){
				GoodPlan goodPlan=service.PrintGoodPlan(obj);
				PrintGoodPlanReport4 p = new PrintGoodPlanReport4();
				p.PrintReady(goodPlan);
			}
		/*	GoodPlan goodPlan=service.PrintGoodPlan(obj);
			obj=service.changeGoodPlan(goodPlan);
				for(int j=0;j<obj.getGoodPlanList().size();j++){
					obj.getGoodPlanList().get(j).setGoodPlan_User(obj.getGoodPlan_User());
					obj.getGoodPlanList().get(j).setGoodPlan_date(obj.getGoodPlan_date());
					obj.getGoodPlanList().get(j).setDoorStore_Name(obj.getDoorStore_Name());
					obj.getGoodPlanList().get(j).setPrint_name(service.queryByDeptName(obj.getGoodPlanList().get(j).getDept_Name()).getPrint_name());
					PrintTool ptool=new PrintTool(obj.getGoodPlanList().get(j));
//					System.out.println(obj.getGoodPlanList().get(j).getDept_Name());
					ptool.printContent();
				}
			*/
			//在这里进行数据的打印
		}
			
	}
	//过滤一下.将查询数据按表头表体转换下.
	public GoodPlan changeGoodPlan(GoodPlan obj){
		List<GoodPlan> goodPlanList=new ArrayList<GoodPlan>();	
		List<Product> tempList=new ArrayList<Product>();
		String dept_name="";
		for(int j=0;j<obj.getProductList().size();j++){
			if(j==0){
				dept_name=obj.getProductList().get(j).getProduct_Dept_Name();
				tempList.add(obj.getProductList().get(j));
			}else{
				if(dept_name.equals(obj.getProductList().get(j).getProduct_Dept_Name())){
					tempList.add(obj.getProductList().get(j));
				}else{
					GoodPlan goodPlan=new GoodPlan();
					goodPlan.setDept_Name(dept_name);
					goodPlan.setProductList(tempList);
					goodPlanList.add(goodPlan);
					tempList=new ArrayList<Product>();
					tempList.add(obj.getProductList().get(j));
					dept_name=obj.getProductList().get(j).getProduct_Dept_Name();
				}					
			}
			if(j==obj.getProductList().size()-1){
				GoodPlan goodPlan=new GoodPlan();
				goodPlan.setDept_Name(dept_name);
				goodPlan.setProductList(tempList);
				goodPlanList.add(goodPlan);
			}
		}
		obj.setGoodPlanList(goodPlanList);
		return obj;
	}
	@SuppressWarnings("unused")
	private void queryReport1(HttpServletRequest request, HttpServletResponse response) {
		GoodPlan obj=new GoodPlan();
		String date=request.getParameter("time");
//		String date=new SimpleDateFormat("yyyy-MM-dd").format(Calendar
//				.getInstance().getTime());
		obj.setGoodPlan_BeginDate(date+" 00:00:00");
		obj.setGoodPlan_EndDate(date+" 23:59:59");
//		obj.setGoodPlan_BeginDate("2012-06-14"+" 00:00:00");
//		obj.setGoodPlan_EndDate("2012-06-14"+" 23:59:59");
		obj.setGoodPlan_Type("1");
		//先获取所有门店的信息
		DoorStoreService doorStoreService=new DoorStoreService();
		List<DoorStore> doorStoreList=doorStoreService.queryAll();
		//获取所有产品信息
		ProductService productService=new ProductService();
		List<Product>  productList=productService.queryAll();
		//查询当日所有门店的原材料供应单的报表情况.
		GoodPlanService goodPlanService=new GoodPlanService();
		List<GoodPlan> goodPlanList=goodPlanService.queryOneDayGoodPlan1(obj);
		//先循环出所有单品
		List<List<Object>> list=new ArrayList<List<Object>>();
		for(int i=0;i<productList.size();i++){
			List<Object> reportItemList=new ArrayList<Object>();
			reportItemList.add("<td>"+productList.get(i).getProduct_Name()+"</td>");
			int sum=0;
			LabelA:
				//循环出所有门店
			for(int j=1;j<doorStoreList.size();j++){
				boolean b=false;
				LabelB:
					//循环所有要货计划表头
					for(int m=0;m<goodPlanList.size();m++){
						//判断要货计划里是否有该门店
						if(goodPlanList.get(m).getDoorStore_Id().equals(doorStoreList.get(j).getDoorStore_Id())){
							//如果有该门店.循环这家门店要货计划明细数据
							List<Product> planBodyList=goodPlanList.get(m).getProductList();
							LabelC:
							for(int o=0;o<planBodyList.size();o++){
								//如果要货明细里和单品对应上,则把数量加进去,否则显示0
								if(productList.get(i).getProduct_Id().equals(planBodyList.get(o).getProduct_Id())){
									reportItemList.add("<td>"+Integer.parseInt(planBodyList.get(o).getProduct_Quantity())+"</td>");
									sum+=Integer.parseInt(planBodyList.get(o).getProduct_Quantity());
									b=true;
								//	System.out.println(planBodyList.get(o).getProduct_Name()+"  "+planBodyList.get(o).getProduct_Quantity());
									break LabelB;
								}
							}
						}
					}
				if(b==false){
					reportItemList.add("<td>0</td>");
				}
			}
			
//			for(Object str:reportItemList){
//				if(!"".equals(str)){
//					System.out.println(str);
//				}
//			}
			reportItemList.add("<td>"+sum+"</td>");
			list.add(reportItemList);
		}
		request.setAttribute("list", list);
		request.setAttribute("doorStoreList", doorStoreList);
	}
	@SuppressWarnings("unused")
	private void queryReport4(HttpServletRequest request, HttpServletResponse response) {
		GoodPlan obj=new GoodPlan();
		String date=request.getParameter("time");
//		String date=new SimpleDateFormat("yyyy-MM-dd").format(Calendar
//				.getInstance().getTime());
		obj.setGoodPlan_BeginDate(date+" 00:00:00");
		obj.setGoodPlan_EndDate(date+" 23:59:59");
//		obj.setGoodPlan_BeginDate("2012-06-14"+" 00:00:00");
//		obj.setGoodPlan_EndDate("2012-06-14"+" 23:59:59");
		obj.setGoodPlan_Type("4");
		//先获取所有门店的信息
		DoorStoreService doorStoreService=new DoorStoreService();
		List<DoorStore> doorStoreList=doorStoreService.queryAll();
		//获取所有产品信息
		BreakfastService breakfastService=new BreakfastService();
		List<Breakfast>  breakfastList=breakfastService.queryAll();
		//查询当日所有门店的原材料供应单的报表情况.
		GoodPlanService goodPlanService=new GoodPlanService();
		List<GoodPlan> goodPlanList=goodPlanService.queryOneDayGoodPlan2(obj);
		//先循环出所有单品
		List<List<Object>> list=new ArrayList<List<Object>>();
		for(int i=0;i<breakfastList.size();i++){
			LabelA:
				//循环早晨趟数
				for(int j=1;j<=breakfastList.get(i).getMorning_tang();j++){
					//循环出所有要货计划
					int sum=0;
					List<Object> reportItemList=new ArrayList<Object>();
					reportItemList.add("<td>"+breakfastList.get(i).getProduct_name()+"</td>");
					reportItemList.add("<td>上午第"+j+"趟</td>");
					LabelB:
					for(int k=1;k<doorStoreList.size();k++){
						//循环出所有门店
						boolean b=false;
//						System.out.println("开始循环门店\t"+doorStoreList.get(k).getDoorStore_Name()+"\t"+"上午"+"第"+j+"趟"+"\t"+breakfastList.get(i).getProduct_name());
						LabelC:
						for(int m=0;m<goodPlanList.size();m++){
							if(goodPlanList.get(m).getDoorStore_Id().equals(doorStoreList.get(k).getDoorStore_Id())){
								//如果有该门店.循环这家门店要货计划明细数据
								List<Breakfast> planBodyList=goodPlanList.get(m).getBreakfastList();
								//循环具体要货内容
								for(int n=0;n<planBodyList.size();n++){
									//判断早晨第几次要货.是否匹配.
									if(planBodyList.get(n).getDept_name().equals(Integer.toString(j))&&"上午".equals(planBodyList.get(n).getRemark())&&planBodyList.get(n).getBreakfast_id()==breakfastList.get(i).getBreakfast_id()){
										reportItemList.add("<td>"+planBodyList.get(n).getQuantity()+"</td>");
										sum+=planBodyList.get(n).getQuantity();
										b=true;
//										System.out.println(doorStoreList.get(k).getDoorStore_Name()+"\t"+planBodyList.get(n).getRemark()+"第"+planBodyList.get(n).getDept_name()+"趟\t"+planBodyList.get(n).getProduct_name()+"\t"+planBodyList.get(n).getQuantity()+"\t 小计"+sum);
										break LabelC;
									}
								}
							}
						}
						if(b==false){
							reportItemList.add("<td>0</td>");
	//						System.out.println("没匹配上"+doorStoreList.get(k).getDoorStore_Name()+"\t"+"上午"+"第"+j+"趟\t"+"\t"+breakfastList.get(i).getProduct_name()+"\t 0个"+"\t 小计"+sum);
						}
					}
					reportItemList.add("<td>"+sum+"</td>");
//					System.out.println("第"+j+"趟匹配结束.加得总和为:"+sum);
//					System.out.println(reportItemList);
					list.add(reportItemList);
				}
		
		//下午的部分
		LabelA:
			//循环下午趟数
			for(int j=1;j<=breakfastList.get(i).getAfternoon_tang();j++){
				//循环出所有要货计划
				int sum=0;
				List<Object> reportItemList=new ArrayList<Object>();
				reportItemList.add("<td>"+breakfastList.get(i).getProduct_name()+"</td>");
				reportItemList.add("<td>下午第"+j+"趟</td>");
				LabelB:
				for(int k=1;k<doorStoreList.size();k++){
					//循环出所有门店
					boolean b=false;
//					System.out.println("开始循环门店\t"+doorStoreList.get(k).getDoorStore_Name()+"\t"+"上午"+"第"+j+"趟"+"\t"+breakfastList.get(i).getProduct_name());
					LabelC:
					for(int m=0;m<goodPlanList.size();m++){
						if(goodPlanList.get(m).getDoorStore_Id().equals(doorStoreList.get(k).getDoorStore_Id())){
							//如果有该门店.循环这家门店要货计划明细数据
							List<Breakfast> planBodyList=goodPlanList.get(m).getBreakfastList();
							//循环具体要货内容
							for(int n=0;n<planBodyList.size();n++){
								//判断早晨第几次要货.是否匹配.
								if(planBodyList.get(n).getDept_name().equals(Integer.toString(j))&&"下午".equals(planBodyList.get(n).getRemark())&&planBodyList.get(n).getBreakfast_id()==breakfastList.get(i).getBreakfast_id()){
									reportItemList.add("<td>"+planBodyList.get(n).getQuantity()+"</td>");
									sum+=planBodyList.get(n).getQuantity();
									b=true;
//									System.out.println(doorStoreList.get(k).getDoorStore_Name()+"\t"+planBodyList.get(n).getRemark()+"第"+planBodyList.get(n).getDept_name()+"趟\t"+planBodyList.get(n).getProduct_name()+"\t"+planBodyList.get(n).getQuantity()+"\t 小计"+sum);
									break LabelC;
								}
							}
						}
					}
					if(b==false){
						reportItemList.add("<td>0</td>");
//						System.out.println("没匹配上"+doorStoreList.get(k).getDoorStore_Name()+"\t"+"上午"+"第"+j+"趟\t"+"\t"+breakfastList.get(i).getProduct_name()+"\t 0个"+"\t 小计"+sum);
					}
				}
				reportItemList.add("<td>"+sum+"</td>");
//				System.out.println("第"+j+"趟匹配结束.加得总和为:"+sum);
//				System.out.println(reportItemList);
				list.add(reportItemList);
			}
		}
//		for(int t=0;t<list.size();t++){
//			for(int v=0;v<list.get(t).size();v++){
//				System.out.print(list.get(t).get(v));
//			}
//			System.out.println();
//		}
		request.setAttribute("list", list);
		request.setAttribute("doorStoreList", doorStoreList);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		doGet(request, response);
	}
}